<template>
  <el-dialog
    :title="!dataForm.rentalId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="出租房名称" prop="rentalName">
      <el-input v-model="dataForm.rentalName" placeholder="出租房名称"></el-input>
    </el-form-item>
    <el-form-item label="出租房标题" prop="rentalTital">
      <el-input v-model="dataForm.rentalTital" placeholder="出租房标题"></el-input>
    </el-form-item>
    <el-form-item label="出租房面积" prop="rentalArea">
      <el-input v-model="dataForm.rentalArea" placeholder="出租房面积"></el-input>
    </el-form-item>
    <el-form-item label="租金" prop="rentalRent">
      <el-input v-model="dataForm.rentalRent" placeholder="租金"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="rentalSort">
      <el-input v-model="dataForm.rentalSort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="状态 0.正常 1.禁用" prop="rentalStatus">
      <el-input v-model="dataForm.rentalStatus" placeholder="状态 0.正常 1.禁用"></el-input>
    </el-form-item>
    <el-form-item label="地区id" prop="areaId">
      <el-input v-model="dataForm.areaId" placeholder="地区id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          rentalId: 0,
          rentalName: '',
          rentalTital: '',
          rentalArea: '',
          rentalRent: '',
          rentalSort: '',
          rentalStatus: '',
          areaId: ''
        },
        dataRule: {
          rentalName: [
            { required: true, message: '出租房名称不能为空', trigger: 'blur' }
          ],
          rentalTital: [
            { required: true, message: '出租房标题不能为空', trigger: 'blur' }
          ],
          rentalArea: [
            { required: true, message: '出租房面积不能为空', trigger: 'blur' }
          ],
          rentalRent: [
            { required: true, message: '租金不能为空', trigger: 'blur' }
          ],
          rentalSort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          rentalStatus: [
            { required: true, message: '状态 0.正常 1.禁用不能为空', trigger: 'blur' }
          ],
          areaId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.rentalId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rentalId) {
            this.$http({
              url: this.$http.adornUrl(`/twohouse/rental/info/${this.dataForm.rentalId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.rentalName = data.rental.rentalName
                this.dataForm.rentalTital = data.rental.rentalTital
                this.dataForm.rentalArea = data.rental.rentalArea
                this.dataForm.rentalRent = data.rental.rentalRent
                this.dataForm.rentalSort = data.rental.rentalSort
                this.dataForm.rentalStatus = data.rental.rentalStatus
                this.dataForm.areaId = data.rental.areaId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/twohouse/rental/${!this.dataForm.rentalId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rentalId': this.dataForm.rentalId || undefined,
                'rentalName': this.dataForm.rentalName,
                'rentalTital': this.dataForm.rentalTital,
                'rentalArea': this.dataForm.rentalArea,
                'rentalRent': this.dataForm.rentalRent,
                'rentalSort': this.dataForm.rentalSort,
                'rentalStatus': this.dataForm.rentalStatus,
                'areaId': this.dataForm.areaId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
